package cn.zh.stan.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.R;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class FinPasst extends BaseActivity {

    ImageView pstback;
    EditText recpass;
    Button reout_time,registn;
    Mycount mycount;
    String name,code;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fin_passt);
        initv();
        }


    public void initv(){
        name=getIntent().getStringExtra("name");
        pstback= (ImageView) findViewById(R.id.pstback);
        pstback.setOnClickListener(this);
        recpass= (EditText) findViewById(R.id.recpass);
        registn= (Button) findViewById(R.id.registn);
        registn.setOnClickListener(this);
        reout_time= (Button) findViewById(R.id.reout_time);
        mycount = new Mycount(60*1000,1000);
        mycount.start();

        reout_time.setEnabled(false);
        reout_time.setOnClickListener(this);
      }



    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.registn:
                getPo();
                break;
            case R.id.pstback:
                finish();
                break;
            case R.id.reout_time:

                break;
        }
    }



    public class Mycount extends CountDownTimer {
        public Mycount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
          }
        @Override
        public void onTick(long millisUntilFinished) {
            long myhour = (millisUntilFinished / 1000) / 3600;
            long myminute = ((millisUntilFinished / 1000) - myhour * 3600) / 60;
            long mysecond = millisUntilFinished / 1000 - myhour * 3600
                    - myminute * 60;
            reout_time.setText(mysecond+"后重发");
        }

        @Override
        public void onFinish() {
            reout_time.setText("重新发送");
            reout_time.setEnabled(true);
          }
      }


//网络请求，验证验证码
    public void getPo(){
        code=recpass.getText().toString();
        OkHttpUtils.post(BaseUrl.pass_check)
                .params("username",name)
                .params("code",code)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("----------------->忘记密码验证验证码==="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                boolean Flag=js.getBoolean("Flag");
                                if (Flag){
                                    String uid=js.getString("UserId");
                                    Intent intent=new Intent(FinPasst.this,FindPth.class);
                                    intent.putExtra("uid",uid);
                                    startActivity(intent);
                                }else {
                                    String msg=js.getString("Msg");
                                    ToastUtil.showShort(FinPasst.this,msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
               }




//发送验证码








}
